package com.easy.ui.config;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.easy.MslApp;
import com.easy.base.model.BaseModel;
import com.easy.base.util.Util;
import com.easy.bean.AbBean;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

public class ConfigModelImpl extends BaseModel implements ConfigModel {
    private String tag = "ConfigModelImpl";
    private String msg = "";
    private AbBean abBean ;

    public void postHandle(final CallBack listener, final int n) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                switch (n) {
                    case -1:
                        listener.onFail(msg);
                        break;
                    case 0:
                        listener.onconfigSuccess(msg);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    public void config(final String url, final String cid, final CallBack listener) {
        try {
            Request request = new Request.Builder().url(url+Util.urlsuffix+"?action=checknet&cid="+cid).build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    msg = Util.netConnectionError;
                    postHandle(listener, -1);
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {//回调的方法执行在子线程。
                        String result = response.body().string();
                        Log.e(tag, result);
                        try {
                            abBean = gson.fromJson(result, AbBean.class);
                            if (null != abBean && abBean.getErrcode() == 0 ) {
                                msg=abBean.getErrmsg();
                                MslApp.sEditor.putString(Util.addk,url);
                                MslApp.sEditor.putString(Util.cidk,cid);
                                MslApp.sEditor.commit();
                                postHandle(listener, 0);
                            }else {
                                msg=abBean.getErrmsg();
                                postHandle(listener, -1);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            msg = Util.exceptionError;
                            postHandle(listener, -1);
                        }
                    } else {
                        msg = Util.netServerError;
                        postHandle(listener, -1);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            msg = Util.exceptionError;
            postHandle(listener, -1);
        }
    }
}

